package com.cardone.platform.common.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.common.dto.OperateLogDto;
import com.google.common.collect.Maps;

/**
 * 操作日志
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class OperateLogJdbcDao implements OperateLogDao {
	@Override
	public int deleteByIds(final OperateLogDto deleteOperateLog) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteOperateLog.getAttrs())) {
			model.putAll(deleteOperateLog.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteOperateLog.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final OperateLogDto findOperateLog) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findOperateLog, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findOperateLog, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findOperateLog.getAttrs())) {
			model.putAll(findOperateLog.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final OperateLogDto findOperateLog) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findOperateLog);

		final Model model = ModelUtils.newModel(new ModelArgs(findOperateLog, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findOperateLog.getAttrs())) {
			model.putAll(findOperateLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final OperateLogDto findListOperateLog) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListOperateLog).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListOperateLog.getAttrs())) {
			model.putAll(findListOperateLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final OperateLogDto insertOperateLog) {
		if (StringUtils.isBlank(insertOperateLog.getId())) {
			insertOperateLog.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertOperateLog));

		if (MapUtils.isNotEmpty(insertOperateLog.getAttrs())) {
			model.putAll(insertOperateLog.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final OperateLogDto paginationOperateLog) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationOperateLog).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationOperateLog.getAttrs())) {
			model.putAll(paginationOperateLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationOperateLog), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final OperateLogDto readOperateLog) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readOperateLog, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readOperateLog.getAttrs())) {
			model.putAll(readOperateLog.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readOperateLog);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readOperateLog, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public Integer readCountByCodeForToDay(final String typeId, final String code) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.typeId.name(), typeId);
		model.put(Attributes.code.name(), code);

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readCountByCodeForToDay.id(), null, model);
	}

	@Override
	public List<Date> readListBeginDateForMax(final String typeId, final String code, final String businessCode) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.typeId.name(), typeId);
		model.put(Attributes.code.name(), code);
		model.put(OperateLogDto.Attributes.businessCode.name(), businessCode);

		return ContextHolder.getBean(JdbcTemplateSupport.class).readList(Date.class, SqlIds.readBeginDateForMax.id(), null, model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final OperateLogDto saveOperateLog) {
		final P oldOperateLog = this.findByCodeOrId(mappedClass, saveOperateLog);

		if (oldOperateLog == null) {
			this.insertByCode(saveOperateLog);

			return this.findByCodeOrId(mappedClass, saveOperateLog);
		}

		final int updateCount = this.updateByCode(saveOperateLog, oldOperateLog);

		if (updateCount < 1) {
			return oldOperateLog;
		}

		return this.findByCodeOrId(mappedClass, saveOperateLog);
	}

	@Override
	public int updateByCode(final OperateLogDto updateOperateLog) {
		final OperateLogDto oldOperateLog = this.findByCodeOrId(OperateLogDto.class, updateOperateLog);

		return this.updateByCode(updateOperateLog, oldOperateLog);
	}

	private <P> int updateByCode(final OperateLogDto updateOperateLog, final P oldOperateLog) {
		if (oldOperateLog == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateOperateLog);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateOperateLog, oldOperateLog, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldOperateLog, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldOperateLog, updateOperateLog, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateOperateLog, updateProperties), new ModelArgs(updateOperateLog, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateOperateLog.getAttrs())) {
			model.putAll(updateOperateLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}

	@Override
	public int updateEndDateForInvalid(final String typeCode, final String code) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.typeCode.name(), typeCode);
		model.put(Attributes.code.name(), code);

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateBeginDateForInvalid.id(), model);
	}
}